#include "mainview.h"
#include "icon.h"
#include <QStyleOptionGraphicsItem>
#include <QApplication>
#include <QDesktopWidget>
#include <QGraphicsSceneMouseEvent>
#include <QtGui>

Icon::Icon(QImage img_on, QImage img_off, QGraphicsScene *scene)
    : QGraphicsRectItem(this, scene)
{
    status = false;

    image_on = new QImage(img_on);
    image_off = new QImage(img_off);

    m_path.addRect(image_on->rect());

    selectedEffect = new QGraphicsDropShadowEffect();
    selectedEffect->setOffset(0,0);
    selectedEffect->setEnabled(false);
    selectedEffect->setBlurRadius(6);
    selectedEffect->setColor(Qt::yellow);

    setGraphicsEffect(selectedEffect);

    setRect(0, 0, image_on->width(), image_on->height());
    setFlag(ItemIsMovable);
    setFlag(ItemIsSelectable);

    float itemScale = (float) QApplication::desktop()->width() / (float)1920;
    setScale(itemScale);
}

void Icon::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
{
       if(!status) {
            p->setRenderHint(QPainter::SmoothPixmapTransform, QPainter::HighQualityAntialiasing);
            p->drawImage( option->exposedRect, *image_off, option->exposedRect, Qt::OrderedAlphaDither );
        }
         else {
            p->setRenderHint(QPainter::SmoothPixmapTransform, QPainter::HighQualityAntialiasing);
            p->drawImage( option->exposedRect, *image_on, option->exposedRect, Qt::OrderedAlphaDither );
        }
        if(isSelected()) {
            QPen pen(Qt::DashLine);
            pen.setColor(Qt::black);
            p->setPen(pen);
            p->setBrush(Qt::NoBrush);
            p->drawPath(m_path);
        }
}

void Icon::changeStatus()
{    
    status = !status;
    Icon::update();
}

QVariant Icon::itemChange(GraphicsItemChange change,
                     const QVariant &value)
{
    if (change == QGraphicsItem::ItemSelectedHasChanged)
        this->updateSelection();
    return value;
}

void Icon::updateSelection()
{
   // if(isSelected())
       // selectedEffect->setEnabled(true);
    //else
       // selectedEffect->setEnabled(false);
}
